﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DMS.Business.Utility;
using Microsoft.Practices.EnterpriseLibrary.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Data;
using System.Text;
using DMS.Base;
using DMS.Business.Modules.Organization;

namespace GMS.Web.Common.Services
{
    public partial class GetUserInfo : System.Web.UI.Page
    {
        private const string UserSql = @"select ou.UserID,ou.UserName,ou.DeptID,od.DeptName
                                        from org_users ou join org_deptments od on ou.deptid=od.deptid
                                            where username like '%{0}%' and od.deptidpath like '%{1}%' order by ou.OrderNo";
        private const string DeptSql = @"select DeptID,DeptName
                                                from org_deptments
                                            where DeptName like '%{0}%' and deptidpath like '%{1}%' order by OrderNo";
        /// <summary>
        /// 返回物资信息XML 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            string methodName = Request.Params["method"];

            if(!string.IsNullOrEmpty(methodName))
            {
                MethodInfo method = this.GetType().GetMethod(methodName);
                method.Invoke(this, null);
            }else
            {
                string id = Request.QueryString["userId"];
                string result = "{\"phone\":\"{0}\",\"deptId\":\"{1}\",\"deptName\":\"{2}\"}";
                string phone = "";
                string deptId = "";
                string deptName = "";
                if (!String.IsNullOrEmpty(id))
                {
                    User user = UserHelper.GetUserByID(id);
                    if (user != null)
                    {
                        phone = user.Mobile;
                        deptId = user.Dept.ID.ToString();
                        deptName = user.Dept.Name; deptName = user.Dept.Name;
                    }
                }
                result = result.Replace("{0}", phone).Replace("{1}", deptId).Replace("{2}", deptName);
                Response.Clear();
                Response.ContentType = "application/json";
                Response.Charset = "UTF-8";
                Response.Write(result);
                Response.End();
            }
        }

        public void GetUserInfoByName()
        {
            string userName = Request.Params["UserName"];
            string deptId = UserHelper.GetTopDeptID();

            string sql = string.Format(UserSql, userName, deptId);

            DataSet ds = DBHelper.Instance.ExecuteDataSet(CommandType.Text, sql);

            string contents = "{\"totalResultsCount\":\"0\",\"UserList\":[";
            if (ds.Tables[0].Rows.Count > 0)
            {
                contents = "";
                contents += "{\"totalResultsCount\":\"" + ds.Tables[0].Rows.Count + "\",\"UserList\":[";
                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                    contents += "{\"UserName\":\"";
                    contents += dr["UserName"].ToString() + "\",";
                    contents += "\"UserID\":\"" + dr["UserID"].ToString() + "\",";
                    contents += "\"DeptName\":\"" + dr["DeptName"].ToString() + "\",";
                    contents += "\"DeptID\":\"" + dr["DeptID"].ToString();
                    contents += "\"},";
                }
                contents = contents.Substring(0, contents.Length - 1);
            }
            contents += "]}";

            Response.Clear();
            Response.ContentType = "application/json";
            Response.Charset = "UTF-8";
            Response.Write(contents);
            Response.End();
        }

        public void GetDeptInfoByName()
        {
            string deptName = Request.Params["DeptName"];
            string deptId = UserHelper.GetTopDeptID();

            string sql = string.Format(DeptSql, deptName, deptId);

            DataSet ds = DBHelper.Instance.ExecuteDataSet(CommandType.Text, sql);

            string contents = "{\"totalResultsCount\":\"0\",\"DeptList\":[";
            if (ds.Tables[0].Rows.Count > 0)
            {
                contents = "";
                contents += "{\"totalResultsCount\":\"" + ds.Tables[0].Rows.Count + "\",\"DeptList\":[";
                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                    contents += "{\"DeptName\":\"";
                    contents += dr["DeptName"].ToString() + "\",";
                    contents += "\"DeptID\":\"" + dr["DeptID"].ToString();
                    contents += "\"},";
                }
                contents = contents.Substring(0, contents.Length - 1);
            }
            contents += "]}";

            Response.Clear();
            Response.ContentType = "application/json";
            Response.Charset = "UTF-8";
            Response.Write(contents);
            Response.End();
        }
    }
}